<template>
  <div>
    <inputs @search="search" @Reset="Reset" @searchByTime="searchByTime" />
    <tables :message="message" @deleOne="deleOne" @deleteAll="deleteAll" />
    <page @thispage="thispage" :leng="leng" :page="page" :pagesize="pagesize" />
    <Dailog @update="update" />
  </div>
</template>

<script>
import inputs from "./lessons/inputs.vue";
import tables from "./lessons/tables.vue";
import page from "./lessons/page.vue";
import Dailog from "./lessons/Dailog.vue";
import {
  getAllData,
  deleteOne,
  searchData,
  deleAllNews,
  updateNews,
  seaByTime,
} from "@/api/lessonapi.js";
export default {
  data() {
    return {
      newss: {},
      type: 0,
      page: 1,
      pagesize: 8,
      message: [],
      leng: 1,
    };
  },
  components: {
    inputs,
    tables,
    page,
    Dailog,
  },
  mounted() {
    this.getAllNews();
    // this.getPageNum();
  },

  methods: {
    // sendnews(obj) {
    //   this.newss = this.obj;
    // },
    // 通过时间查找
    searchByTime(val) {
      this.newss = val;
      var { value1, value2 } = this.newss;
      seaByTime({
        page: this.page,
        pagesize: this.pagesize,
        value1,
        value2,
      }).then((res) => {
        if (res.data) {
          this.type = 1;
          this.message = res.data;
          this.leng = res.mess;
        } else {
          this.message = res.data;
        }
      });
    },
    // 修改
    update(val) {
      updateNews(val).then((res) => {
        this.type = 0;
        this.getAllNews();
      });
    },
    //  批量删除
    deleteAll(val) {
      // console.log(6666, val);
      deleAllNews(val).then((res) => {
        this.type = 0;
        this.getAllNews();
      });
    },
    Reset() {
      this.type = 0;
      this.getAllNews();
    },
    // 搜索
    search(val) {
      searchData(val).then((res) => {
        console.log(res == false);
        if (!res) {
          this.getAllNews();
        } else {
          this.type = 0;
          this.message = res.data;
          this.leng = res.news;
        }
      });
    },
    // 删除一条数据
    deleOne(val) {
      deleteOne(val).then((res) => {
        console.log(res);
        this.type = 0;
        this.getAllNews();
      });
    },
    // 获取分页数据
    thispage(val) {
      this.page = val;
      if (this.type) {
        this.searchByTime(this.newss);
      } else {
        this.getAllNews();
      }
    },
    // getPageNum() {
    //   getPageNums({ page: this.page, pagesize: this.pagesize }).then((res) => {
    //     console.log(res.data);
    //   });
    // },
    getAllNews() {
      getAllData({ page: this.page, pagesize: this.pagesize }).then((res) => {
        this.type = 0;
        this.message = res.data;
        this.leng = res.news;
      });
    },
  },
};
</script>

<style>
</style>